#import "CTGuideVC.h"
#import "CTGuidePageControl.h"
@interface CTGuideVC ()<UIScrollViewDelegate>
@property (nonatomic ,strong) UIScrollView *scrollView;
@property (nonatomic ,strong) CTGuidePageControl *pageControl;
@end
static NSInteger numOfPage = 4;
@implementation CTGuideVC
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNeedsStatusBarAppearanceUpdate];
    [self addSubViews];
    [self setConstraints];
    [self setAnimation];
}
- (void)addSubViews
{
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageControl];
}
- (void)setConstraints
{
    [self.scrollView setFrame:self.view.bounds];
    CGFloat width = 200;
    CGFloat x = (kScreenWidth-width)/2;
    CGFloat y = kScreenHeight-165;
    if (!kIsBangsScreen) {
        y = kScreenHeight - 90;
    }
    [self.pageControl setFrame:CGRectMake(x, y, width, 20)];
}
- (void)setAnimation{
        self.handleToHome();
}
- (void)setGuidePage
{
    for (int i = 0; i < numOfPage; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"Welcome_%d",i]];
        imageView.ct_size = _scrollView.ct_size;
        imageView.ct_left = i * _scrollView.ct_width;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        [_scrollView addSubview:imageView];
        if (i == numOfPage - 1) {
            [self setupLastPageWithView:imageView];
        }
        else
        {
            [self setupFrontPageWithView:imageView];
        }
    }
}
- (void)setupLastPageWithView:(UIImageView *)imageView{
    imageView.userInteractionEnabled = true;
    CGFloat width = 200;
    CGFloat x = (kScreenWidth-width)/2;
    CGFloat y = kScreenHeight-100;
    if (!kIsBangsScreen) {
        y = kScreenHeight - 55;
    }
    UIButton *enterButton = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, 40)];
    [enterButton setTitleColor:[UIColor color222] forState:UIControlStateNormal];
    enterButton.titleLabel.font = kFontSize(16);
    enterButton.layer.masksToBounds = YES;
    enterButton.layer.cornerRadius = 5.0;
    enterButton.layer.borderColor = [UIColor color222].CGColor;
    enterButton.layer.borderWidth = 1.0;
    [enterButton setTitle:@"Immediate experience" forState:UIControlStateNormal];
    [enterButton addTarget:self action:@selector(enterBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:enterButton];
}
- (void)setupFrontPageWithView:(UIImageView *)imageView
{
    imageView.userInteractionEnabled = true;
    UIButton *enterButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 100, kStatusBarHeight, 100, 100)];
    enterButton.titleLabel.font = kFontSize(12);
    [enterButton setTitle:@"skip" forState:UIControlStateNormal];
    [enterButton setTitleColor:[UIColor color222] forState:UIControlStateNormal];
    [enterButton addTarget:self action:@selector(enterBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:enterButton];
}
#pragma mark - event handlers
- (void)enterBtnClick:(UIButton *)button{
    self.handleToHome();
}
#pragma mark - Delegates
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
}
#pragma mark - systemDelegates
#pragma mark lazyLoading
- (UIScrollView *)scrollView
{
    if (_scrollView ) {
        return _scrollView;
    }
    _scrollView = [UIScrollView new];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.contentSize = CGSizeMake(kScreenWidth * numOfPage, kScreenHeight);
    _scrollView.delegate = self;
    return _scrollView;
}
- (CTGuidePageControl *)pageControl
{
    if (_pageControl == nil) {
        _pageControl = [[CTGuidePageControl alloc] init];
        _pageControl.grayColor = [UIColor color222];
        _pageControl.numberOfPages = 4;
        _pageControl.currentPage = 0;
    }
    return _pageControl;
}
@end
